//
//  ViewController.m
//  05-UI
//
//  Created by Snow on 16/7/20.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "Album.h"
#import "ScrollView.h"
#define ScreenBounds [UIScreen mainScreen].bounds
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;

@end
BOOL isOut = NO;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     引导页
     */
    NSUserDefaults *user = [[NSUserDefaults alloc] init];
    if (![user objectForKey:@"yiyi"]) {
        //放图片
        [self makeLaunchView];
        [self addPage];
        [user setObject:@"" forKey:@"yiyi"];
    }
    
    /**
     轮播图
     */
    NSArray *array = @[@"1", @"2", @"3", @"4"];
    NSMutableArray *mArr = [NSMutableArray array];
    for (int i = 0; i < array.count; i++) {
        UIImage *image = [UIImage imageNamed:array[i]];
        [mArr addObject:image];
    }
    Album *album = [[Album alloc] initWithFrame:self.view.bounds images:mArr];
    [self.view addSubview:album];
}
- (void)makeLaunchView{
    NSArray *arr= [NSArray arrayWithObjects:@"1", @"2", @"3", @"4" , nil];
    //通过scrollView 将这些图片添加上
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scroll.contentSize = CGSizeMake(WIDTH * arr.count, HEIGHT);
    //分页效果
    scroll.pagingEnabled = YES;
    //监听滑动
    scroll.delegate = self;
    [self.view addSubview:scroll];
    for (int i = 0; i < arr.count; i++) {
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arr[i]]];
        image.frame = CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT);
        [scroll addSubview:image];
    }
}
- (void)addPage
{
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    //当前页
    self.page.currentPage = 0;
    self.page.numberOfPages = 4;
    [self.view addSubview:self.page];
}
//scrollview 的协议方法
//第一类 监听滚动 让图控制:吧偏移量转化为contentoffet为currentpage
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > 3 * WIDTH + 30) {
        isOut = YES;
        
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    //
    self.page.currentPage = index;
    //判断isOut为真 则进入主界面
    if (isOut) {
        //添加动画
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha = 0;//scrollView渐变消失
        }completion:^(BOOL finished) {
            [scrollView removeFromSuperview];//移除scrollView
            self.page.hidden = YES;
        } ];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
